package com.dhcc.sds.busi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.sds.base.dao.SdsQcentityRuleMapper;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.busi.dao.SdsQcincaseMapper;
import com.dhcc.sds.busi.dto.AnalysisExclusionDto;
import com.dhcc.sds.busi.dto.AnalysisSdsMstaLocDto;
import com.dhcc.sds.busi.service.IAnalysisSdsExclusionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AnalysisSdsExclusionServiceImpl implements IAnalysisSdsExclusionService {

    @Resource
    private SdsQcincaseMapper sdsQcincaseMapper;
    @Resource
    private SdsQcentityRuleMapper sdsQcentityRuleMapper;
    @Override
    public JSONObject selectSdsExclusionTable(Map<String, Object> params) {
        JSONObject obj = new JSONObject();
        List<AnalysisExclusionDto> statisList = sdsQcincaseMapper.selectSdsExclusionTable(params);
        for (AnalysisExclusionDto exclusionDto : statisList){
            if (StringUtils.isEmpty(exclusionDto.getOperReason())){
                if (!StringUtils.isEmpty(exclusionDto.getOperReason())){
                    String reason = exclusionDto.getOperReason();
                    if (reason.contains(",")){

                    }
                }
            }
        }
        obj.put("tableData", statisList);
        return obj;
    }

    @Override
    public List<AnalysisExclusionDto> pageExclusionTable(Page<AnalysisExclusionDto> page, Map<String, Object> params) {
        List<AnalysisExclusionDto> exclusionDtos = sdsQcincaseMapper.selectSdsExclusionTable(page, params);
        for (AnalysisExclusionDto exclusionDto : exclusionDtos){
            String operReason = exclusionDto.getOperReason();
            String exclusionReason = "";
            if (!StringUtils.isEmpty(operReason)){
                if (operReason.contains(",")){
                    String[] split = operReason.split(",");
                    for (String s : split){
                        SdsQcentityRuleEntity sdsQcentityRuleEntity = sdsQcentityRuleMapper.selectById(Long.parseLong(s));
                        if (!StringUtils.isEmpty(sdsQcentityRuleEntity)){
                            exclusionReason = exclusionReason + ";" +sdsQcentityRuleEntity.getRuleDesc();
                        }
                    }
                    if ("" != exclusionReason) {
                        exclusionReason = exclusionReason.substring(1, exclusionReason.length());
                    }
                }else {
                    SdsQcentityRuleEntity sdsQcentityRuleEntity = sdsQcentityRuleMapper.selectById(Long.parseLong(operReason));
                    if (!StringUtils.isEmpty(sdsQcentityRuleEntity)){
                        exclusionReason = sdsQcentityRuleEntity.getRuleDesc();
                    }
                }
            }
            exclusionDto.setOperReason(exclusionReason);
        }
        return exclusionDtos;
    }
}
